﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using Landpy.LogAnalyzer.Configuration;

namespace Landpy.LogAnalyzer.Log
{
    internal class LogWriter : ILogWriter
    {
        private readonly string logFileName;

        public LogWriter(string logFileName)
        {
            this.logFileName = logFileName;
        }

        #region ILogWriter Members

        public void Write(List<LogRow> logRows)
        {
            using (TextWriter textWriter = new StreamWriter(logFileName))
            {
                foreach (LogRow logRow in logRows)
                {
                    var stringBuilder = new StringBuilder();
                    foreach (string columnHeaderText in ConfigurationSession.GetSession().Setting.ColumnHeaderTexts)
                    {
                        stringBuilder.AppendFormat("{0}: {1};", columnHeaderText, logRow[columnHeaderText]);
                    }
                    textWriter.WriteLine(stringBuilder.ToString());
                }
                textWriter.Close();
            }
        }

        #endregion
    }
}